/**
 * Copyright (c) 2016-2021 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.zsmartsystems.zigbee.zdo.command;

import javax.annotation.Generated;

import com.zsmartsystems.zigbee.ZigBeeAddress;
import com.zsmartsystems.zigbee.ZigBeeBroadcastDestination;
import com.zsmartsystems.zigbee.ZigBeeCommand;
import com.zsmartsystems.zigbee.ZigBeeEndpointAddress;
import com.zsmartsystems.zigbee.transaction.ZigBeeTransactionMatcher;
import com.zsmartsystems.zigbee.zcl.ZclFieldDeserializer;
import com.zsmartsystems.zigbee.zcl.ZclFieldSerializer;
import com.zsmartsystems.zigbee.zcl.protocol.ZclDataType;
import com.zsmartsystems.zigbee.zdo.ZdoRequest;
import com.zsmartsystems.zigbee.zdo.ZdoResponse;
import com.zsmartsystems.zigbee.zdo.command.ManagementPermitJoiningResponse;

/**
 * Management Permit Joining Request value object class.
 * <p>
 * <p>
 * The Mgmt_Permit_Joining_req is generated from a Local Device requesting that a remote
 * device or devices allow or disallow association. The Mgmt_Permit_Joining_req is
 * generated by a management application or commissioning tool which directs the request to a
 * remote device(s) where the NLME-PERMIT-JOINING.request is executed using the
 * PermitDuration parameter supplied by Mgmt_Permit_Joining_req. Additionally, if the
 * remote device is the Trust Center and TC_Significance is set to 1, the Trust Center
 * authentication policy will be affected. The addressing may be unicast or "broadcast to all
 * routers and coordinator".
 * <p>
 * Code is auto-generated. Modifications may be overwritten!
 */
@Generated(value = "com.zsmartsystems.zigbee.autocode.ZigBeeCodeGenerator", date = "2020-12-25T10:11:19Z")
public class ManagementPermitJoiningRequest extends ZdoRequest implements ZigBeeTransactionMatcher {
    /**
     * The ZDO cluster ID.
     */
    public static int CLUSTER_ID = 0x0036;

    /**
     * Permit Duration command message field.
     */
    private Integer permitDuration;

    /**
     * Tc_Significance command message field.
     */
    private Boolean tcSignificance;

    /**
     * Default constructor.
     *
     * @deprecated from release 1.3.0. Use the parameterised constructor instead of the default constructor and setters.
     */
    @Deprecated
    public ManagementPermitJoiningRequest() {
        clusterId = CLUSTER_ID;
    }

    /**
     * Constructor providing all required parameters.
     *
     * @param permitDuration {@link Integer} Permit Duration
     * @param tcSignificance {@link Boolean} Tc_Significance
     */
    public ManagementPermitJoiningRequest(
            Integer permitDuration,
            Boolean tcSignificance) {

        clusterId = CLUSTER_ID;

        this.permitDuration = permitDuration;
        this.tcSignificance = tcSignificance;
    }

    /**
     * Gets Permit Duration.
     *
     * @return the Permit Duration
     */
    public Integer getPermitDuration() {
        return permitDuration;
    }

    /**
     * Sets Permit Duration.
     *
     * @param permitDuration the Permit Duration
     * @deprecated as of 1.3.0. Use the parameterised constructor instead to ensure that all mandatory fields are provided.
     */
    @Deprecated
    public void setPermitDuration(final Integer permitDuration) {
        this.permitDuration = permitDuration;
    }

    /**
     * Gets Tc_Significance.
     *
     * @return the Tc_Significance
     */
    public Boolean getTcSignificance() {
        return tcSignificance;
    }

    /**
     * Sets Tc_Significance.
     *
     * @param tcSignificance the Tc_Significance
     * @deprecated as of 1.3.0. Use the parameterised constructor instead to ensure that all mandatory fields are provided.
     */
    @Deprecated
    public void setTcSignificance(final Boolean tcSignificance) {
        this.tcSignificance = tcSignificance;
    }

    @Override
    public void serialize(final ZclFieldSerializer serializer) {
        super.serialize(serializer);

        serializer.serialize(permitDuration, ZclDataType.UNSIGNED_8_BIT_INTEGER);
        serializer.serialize(tcSignificance, ZclDataType.BOOLEAN);
    }

    @Override
    public void deserialize(final ZclFieldDeserializer deserializer) {
        super.deserialize(deserializer);

        permitDuration = (Integer) deserializer.deserialize(ZclDataType.UNSIGNED_8_BIT_INTEGER);
        tcSignificance = (Boolean) deserializer.deserialize(ZclDataType.BOOLEAN);
    }

    @Override
    public boolean isTransactionMatch(ZigBeeCommand request, ZigBeeCommand response) {
        if (!(response instanceof ManagementPermitJoiningResponse)) {
            return false;
        }

        ZigBeeAddress destinationAddress = ((ZdoRequest) request).getDestinationAddress();
        ZigBeeAddress sourceAddress = ((ZdoResponse) response).getSourceAddress();
        ZigBeeEndpointAddress localCoordinator = new ZigBeeEndpointAddress(0, 0);

        if(!ZigBeeBroadcastDestination.isBroadcast(destinationAddress.getAddress())) {
            if (!localCoordinator.equals(sourceAddress) && !destinationAddress.equals(sourceAddress)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder(101);
        builder.append("ManagementPermitJoiningRequest [");
        builder.append(super.toString());
        builder.append(", permitDuration=");
        builder.append(permitDuration);
        builder.append(", tcSignificance=");
        builder.append(tcSignificance);
        builder.append(']');
        return builder.toString();
    }

}
